/**
 * KopsoxFramework
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.util.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.kopsox.comm.message.ErrorMessage;
import org.kopsox.data.ValidateableValue;
import org.kopsox.data.container.ValueContainer;
import org.kopsox.data.impl.Polymorph;
import org.kopsox.environment.proxy.KopsoxRegistry;

/**
 * This util provides common tools for changing Values in Containers
 * 
 * @author Konrad Renner - 16.11.2010
 */
public final class ContainerUtil {

	private ContainerUtil() {
		//Utility class
	}
	
	/**
	 * Loads the ValueContainer with the given name from the ContainerEnvironment
	 * 
	 * @param name - String
	 * @return ValueContainer
	 */
	public static ValueContainer getContainerFromEnvironment(String name) {
		return KopsoxRegistry.INSTANCE.getContainerEnvironment().getValueContainer(name);
	}
	
	/**
	 * Maps all Values from the Map into the Container
	 * 
	 * @param newValues
	 * @param container
	 * @return List<ErrorMessage>
	 */
	public static List<ErrorMessage> copyStringMapIntoContainer(Map<String, String> newValues,ValueContainer container){
		List<ErrorMessage> ret = new ArrayList<ErrorMessage>();
		
		Set<Entry<String, String>> entries = newValues.entrySet();
		
		for(Entry<String, String> entry:entries) {
			ret.addAll(container.setValue(entry.getKey(), new Polymorph<Serializable>(entry.getValue())));
		}
		
		return ret;
	}
	
	/**
	 * Maps all Values from the Map into the Container
	 * 
	 * @param newValues
	 * @param container
	 * @return List<ErrorMessage>
	 */
	public static List<ErrorMessage> copyValueMapIntoContainer(Map<String, ValidateableValue<Serializable>> newValues,ValueContainer container){
		List<ErrorMessage> ret = new ArrayList<ErrorMessage>();
		
		Set<Entry<String, ValidateableValue<Serializable>>> entries = newValues.entrySet();
		
		for(Entry<String, ValidateableValue<Serializable>> entry:entries) {
			ret.addAll(container.setValue(entry.getKey(), entry.getValue()));
		}
		
		return ret;
	}
	
	/**
	 * Maps all Values from the Collection into the Container
	 * 
	 * @param newValues
	 * @param container
	 * @return List<ErrorMessage>
	 */
	public static List<ErrorMessage> copyValueCollectionIntoContainer(Collection<ValidateableValue<Serializable>> newValues,ValueContainer container){
		List<ErrorMessage> ret = new ArrayList<ErrorMessage>();
		
		
		for(ValidateableValue<Serializable> val :newValues) {
			ret.addAll(container.setValue(val.getValueDescriptor().getName(), val));
		}
		
		return ret;
	}
}
